package com.oa.controller;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oa.aop.LogAnnotation;
import com.oa.bean.FileCabinet;
import com.oa.bean.PublicFile;
import com.oa.service.FileCabinetService;

@Controller
public class FileCabinetController {

	@Resource
	FileCabinetService fileCabinetService;
	
	@RequestMapping("addMyFile")
	@ResponseBody
	@LogAnnotation(description="增加文件")
	public String addMyFile(MultipartFile file,HttpSession session){
		String userName = (String) session.getAttribute("username");
		Integer uid = (Integer) session.getAttribute("uid");
		String fileName="";
		if(!"".equals(file.getOriginalFilename())){
			fileName = file.getOriginalFilename();			
			String uploadPath="C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\personal-file\\"+userName+"\\"+fileName;//设置上传路径
			System.out.println("上传路径"+uploadPath);
			try {
				file.transferTo(new File(uploadPath));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		FileCabinet fileCabinet = new FileCabinet();
		fileCabinet.setFileName(fileName);
		fileCabinet.setPost_id(uid);
		String result = fileCabinetService.addMyFile(fileCabinet);
		return result;
	}
	
	@RequestMapping("downMyFile")
	public String downMyFile(String fileName,HttpSession session,HttpServletResponse response){
		System.out.println(fileName);
		String userName = (String) session.getAttribute("username");
		System.out.println(userName);
		
		if(!"null".equals(fileName)){
			response.setCharacterEncoding("UTF-8");
			response.setContentType("multipart/form-data");
			String realPath = "C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\personal-file\\"+userName+"\\"+fileName;
			try {
				response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
				OutputStream os = response.getOutputStream();
				os.write(FileUtils.readFileToByteArray(new File(realPath)));
				os.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		System.out.println("jieshu");
		return null;
	}
	
	@RequestMapping("getMyFileList")
	@ResponseBody
	public List<FileCabinet> getMyFileList(HttpSession session){
		Integer uid = (Integer) session.getAttribute("uid");
		List<FileCabinet> list = fileCabinetService.getMyFileList(uid);
		return list;
	}
	
	@RequestMapping("deleteMyFile")
	@ResponseBody
	public String deleteMyFile(String fileName,HttpSession session,Integer fid){
		String userName = (String) session.getAttribute("username");
		File file = new File("C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\personal-file\\"+userName+"\\"+fileName);
		if(file.exists() && file.isFile()){
			file.delete();
		}
		String result = fileCabinetService.moveMyFile(fid);
		return result;
	}
	
	@RequestMapping("increaseFlie")
	@ResponseBody
	public String increaseFlie(MultipartFile file,HttpSession session){
		Integer deptno = (Integer) session.getAttribute("deptno");
		Integer uid = (Integer) session.getAttribute("uid");
		PublicFile publicFile = new PublicFile();
		publicFile.setDeptno(deptno);
		publicFile.setUserId(uid);
		String fileName;
		if(!"".equals(file.getOriginalFilename())){
			fileName = file.getOriginalFilename();			
			String uploadPath="C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\public-file\\"+fileName;//设置上传路径
			System.out.println("上传路径"+uploadPath);
			try {
				file.transferTo(new File(uploadPath));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			publicFile.setName(fileName);
		}
		String result = fileCabinetService.addPublicFile(publicFile);
		return result;
	}
	//管理公文
	@RequestMapping("marginFileList")
	@ResponseBody
	public Map<String,Object> marginFileList(HttpSession session,Integer condition){
		System.out.println(condition);
		if(condition==null){
			condition=0;
		}
		System.out.println(condition);
		Integer deptno = (Integer) session.getAttribute("deptno");
		Map<String,Object> map = fileCabinetService.managePublicFileList(deptno,condition);
		
		return map;
	}
	
	@RequestMapping("getPublicList")
	@ResponseBody
	public List<PublicFile> getPublicFileList(HttpSession session){
		Integer deptno = (Integer) session.getAttribute("deptno");
		List<PublicFile>list = fileCabinetService.getPublicFileList(deptno);
		return list;
	}
	
	//删除公文
	@RequestMapping("deletePublicFile")
	@ResponseBody
	public String deletePublicFile(Integer fid){
		System.out.println("文件的id是"+fid);
		String result = fileCabinetService.movePublicFile(fid);
		return result;
	}
	
	@RequestMapping("downPublicFile")
	public String downPublicFile(String fileName,HttpServletResponse response){
		System.out.println(fileName);
		
		
		if(!"null".equals(fileName)){
			response.setCharacterEncoding("UTF-8");
			response.setContentType("multipart/form-data");
			String realPath = "C:\\Users\\longer\\git\\OAXiTongGuanLi\\src\\main\\webapp\\public-file\\"+fileName;
			try {
				response.setHeader("Content-Disposition", "attachment;fileName="+ new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
				OutputStream os = response.getOutputStream();
				os.write(FileUtils.readFileToByteArray(new File(realPath)));
				os.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		System.out.println("jieshu");
		return null;
	}
	
}
